package Test;

import entity.Person;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class GetClassMethodsInfo {
    public static void main(String[] args) {
        Method[] methods = Person.class.getDeclaredMethods();
        System.out.println("===============方法展示================");
        for (Method m : methods) {
            System.out.println("方法名：" + m.getName());
            System.out.println("返回值类型：" + m.getReturnType().getName());
            Class[] params = m.getParameterTypes();
            if (params.length == 0) {
                System.out.println("无参方法");
            } else {
                System.out.print("参数列表：[");
                for (int i = 0; i < params.length; i++) {
                    if (i != 0)
                        System.out.println(", ");
                    System.out.println(params[i].getName());
                }
                System.out.println("]");
            }
            System.out.println("访问修饰符");
            int modifier = m.getModifiers();
            if ((modifier & Modifier.PUBLIC) == Modifier.PUBLIC)
                System.out.println("public");
            else if ((modifier & Modifier.PROTECTED) == Modifier.PROTECTED)
                System.out.println("protected");
            else if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE)
                System.out.println("private");
            else
                System.out.println("default(package)");
            if ((modifier & Modifier.STATIC) == Modifier.STATIC)
                System.out.println("这是一个静态方法");
            if ((modifier & Modifier.FINAL) == Modifier.FINAL)
                System.out.println("这是一个final方法");
            if ((modifier & Modifier.ABSTRACT) == Modifier.ABSTRACT)
                System.out.println("这是一个抽象方法");
            if ((modifier & Modifier.SYNCHRONIZED) == Modifier.SYNCHRONIZED)
                System.out.println("这是同步方法");
            Class declaringClass = m.getDeclaringClass();
            System.out.println("方法声明在：" + declaringClass.getName());
            Class[] exceptions = m.getExceptionTypes();
            if (exceptions.length > 0) {
                System.out.print("该方法抛出的异常有：[");
                for (int i = 0; i < exceptions.length; i++) {
                    if (i != 0)
                        System.out.println(",");
                    System.out.println(exceptions[i].getName());
                }
                System.out.println("]");
            }
            System.out.println("-------------------------");
        }
    }
}
